#include "../comm.h"
class Solution {
public:
    int longestEqualSubarray(vector<int>& nums, int k) {
        unordered_map<int, vector<int>> hsh;
        for (int i = 0; i < nums.size(); ++i)
            hsh[nums[i]].push_back(i);
        int ans = 0;
        for (auto& [num, v] : hsh)
        {
            int now = 1, sep = 0;
            for (int st = 0, ed = 1; ed < v.size(); ++ed)
            {
                sep += v[ed] - v[ed - 1] - 1;
                while (sep > k)
                {
                    sep -= v[st + 1] - v[st] - 1;
                    st++;
                }
                now = max(now, ed - st + 1);
            }
            ans = max(ans, now);
        }
        return ans;
    }
};